package beerAgent;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import flatlandAgent.FlatlandParameters;
import genotype.BeerGenotype;
import gui.SettingPanel;

public class BeerSettingPanel extends SettingPanel{

	private static final long serialVersionUID = 1L;
	private JLabel hiddenLayers;
	private JTextField layers;
	private JCheckBox horizontalSpeed; 
	
	public BeerSettingPanel(BeerProblem problem, BeerParameters parameters) {
		super(problem,parameters); 
	}
	
	@Override
	protected void initComponents(){
		super.initComponents(); 

		
		hiddenLayers = new JLabel("Hidden layers");
		addComponent(hiddenLayers, 0, 5); 
		addHiddenLayerField();
		
		hiddenLayers = new JLabel("Horizontal speed");
		addComponent(hiddenLayers, 2, 5);
		
		horizontalSpeed = new JCheckBox(); 
//		horizontalSpeed.addActionListener(new HorizontalSpeedActionListner()); 
		addComponent(horizontalSpeed, 3, 5); 
		
	}

	private void addHiddenLayerField() {
		layers = new JTextField();
		layers.setColumns(10);
		GridBagConstraints constraint = new GridBagConstraints(); 
		constraint.gridx = 1; 
		constraint.gridy = 5; 
		constraint.gridwidth = 3; 
		constraint.anchor = GridBagConstraints.WEST; 
		this.add(layers, constraint);
	}
	
	@Override
	protected void readParameters(){
		super.readParameters(); 
		BeerParameters parameters = ((BeerParameters) this.parameters); 
		parameters.setNetworkLayout(setLayers());
		parameters.setHorizontalSpeed(horizontalSpeed.isSelected()); 
	}
	
	@Override
	public void showDefaultParameters(){
		super.showDefaultParameters(); 
		layers.setText(getHiddenLayers());
		horizontalSpeed.setSelected(((BeerParameters)parameters).getHorizontalSpeed()); 
	}
	
	private String getHiddenLayers() {
		String s = "";
		int[] layout = ((BeerParameters) parameters).getNetworkLayout();
		
		if (layout.length == 2)
			return "";
		
		for (int i = 1; i < layout.length-1; i++) {
			s += Integer.toString(layout[i])+ ",";
		}
		s = s.substring(0, s.length()-1);
		return s;
	}
	
	private int[] setLayers() {
		BeerParameters parameters = (BeerParameters) this.parameters; 
		String hidden = layers.getText().replaceAll("\\s", "");
		String returnString = "";
		String input = Integer.toString(parameters.getNetworkLayout()[0]);
		String output = Integer.toString(parameters.getNetworkLayout()[parameters.getNetworkLayout().length-1]);

		if (hidden.length() == 0)
			returnString = input + "," + output;
		else returnString = input + "," + hidden + "," + output;

		String[] layersString = returnString.split(",");
		int[] layersInt = new int[layersString.length];
		for (int i = 0; i < layersString.length; i++) {
			if (! layersString[i].equals("") )
					layersInt[i] = Integer.parseInt(layersString[i]);
		}
//		System.out.println("Setlayers:" + Arrays.toString(layersInt));
		return layersInt;
	}
	
}
